
function EFormSetValue(sN,sV) {
  sN = GetFormFieldIDByName(sN);  
  if (sV=='') ep_cfld(sN); 
  else {    
  var f=document.HtmlPlusForms[sN]; fid=GetFormFieldIndexByID(sN); 
  if (fid<0) fid=GetFormFieldIndexByID(String(sN+':0')); // checkbox  
  if (fid>-1) {
    if (HtmlFormFields[fid].type=='select') {    
          var ix=0;var bChk=false;var f = document.HtmlPlusForms[HtmlFormFields[fid].name][ix];
            while (f) {   
               f = document.HtmlPlusForms[HtmlFormFields[fid].name][ix];
               if (f) { 
                  if (f.text==sV) {f.selected=true;break;}           
               }
               ix++;
            }                  
    }                    
    if (HtmlFormFields[fid].type!='checkbox'&&HtmlFormFields[fid].type!='select') {
		f.value=sV; if (HtmlFormFields[fid].type=='hidden' && sN.substring(0,4)=='PCX_') 
			if (document.getElementById('img_' + sN.substring(4))) { 
				if (trim(sV).length>0) { setTimeout("$('#img_" + sN.substring(4) + "').attr('src','" + trim(sV) + "');", 500);	}				
			}
	}
    else {   // sc, mc felder
        if (HtmlFormFields[fid].subtype=='sc') {
            var ix=0;var bChk=false;var f = document.HtmlPlusForms[HtmlFormFields[fid].name][ix];
            while (f) {   
			   f = document.HtmlPlusForms[HtmlFormFields[fid].name][ix];			   
               if (f) { 
                  if (f.value==sV) {f.checked=true;break;}           
               }
               ix++;
            }                
        }
        else {
           var ix=0;var fb=String(HtmlFormFields[fid].name);fb=fb.substring(0,fb.indexOf(':'));var f = document.HtmlPlusForms[fb+':'+ix];          
            while (f) {                         
               f = document.HtmlPlusForms[fb+':'+ix];
               if (f) { 
					EPRT(f);
					if (sV.indexOf(f.value)>=0) f.checked=true; else f.checked=false; 	               
               }
               ix++;
            }     
        }                
    }
  }                            
 }
  EP_HL_REQ_FLD(sN); 
}

function EFormGetValue(sN) {  
  sN = GetFormFieldIDByName(sN); fid=GetFormFieldIndexByID(sN); var rv="";var re=0;var f=document.HtmlPlusForms[sN];    
  if (fid>-1) {
    if (HtmlFormFields[fid].type=='select') {
    var ix=0;var bChk=false;var f = document.HtmlPlusForms[HtmlFormFields[fid].name][ix];
            while (f) {   
               f=document.HtmlPlusForms[HtmlFormFields[fid].name][ix];		   
               if (f) if (f.selected) {rv=f.text;break;}           
               ix++;
            }
    }                            
    if (HtmlFormFields[fid].type!='checkbox'&&HtmlFormFields[fid].type!='select') rv=f.value;
    else {   // sc, mc felder    
        if (HtmlFormFields[fid].subtype=='sc') {
            var ix=0;var bChk=false;var f = document.HtmlPlusForms[HtmlFormFields[fid].name][ix];
            while (f) {   
   			   f = document.HtmlPlusForms[HtmlFormFields[fid].name][ix];
               if (f) if (f.checked) {rv=f.value;break;}           
               ix++;
            }                
        }
        else {
            var ix=0;var fb=String(HtmlFormFields[fid].name);fb=fb.substring(0,fb.indexOf(':'));			
			var f = document.HtmlPlusForms[fb+':'+ix];
            while (f) {                         
               f = document.HtmlPlusForms[fb+':'+ix];
               if (f) if (f.checked) rv+=f.value+"\t";
               ix++;
            }
            if (ix>=1) rv=rv.substring(0,rv.length-1);                                     
        }                
    }
   }   
   if (isNumber(rv)&&HtmlFormFields[fid].tan=='1') return Number(rv); else return String(rv);   
}

function EFormGetPercentFilled(fI) {
	var cF=0;
	try {
		var element = document.getElementById(fI); var context = element.getContext("2d");
		var width = parseInt(element.getAttribute("width")); var height = parseInt(element.getAttribute("height"));
		var imgd = context.getImageData(0, 0, width, height);var pix = imgd.data;		
		var cP=0;var n=0;var mP=pix.length/4; 
		for (var i = 0, n = pix.length; i < n; i += 4) {
			if (pix[i]==0&&pix[i+1]==0&&pix[i+2]==0) cP++; 
		}
		cF=100/mP*cP; cF=Math.round(cF * 100) / 100; 
	}
	catch (e) { }
	return parseFloat(cF);	
}


function EFormGetRequired(sN) {
  var rv=false;
  for (var i = 0; i < HtmlFormFields.length; i++) {
    if (HtmlFormFields[i].name==sN || HtmlFormFields[i].name==sN+':0') { 
      if (HtmlFormFields[i].required==1) rv=true; break; 
    }
  }  
  return rv;
}

function EFormSetRequired(sN,sSTATE) {
  for (var i = 0; i < HtmlFormFields.length; i++) {
    if (HtmlFormFields[i].name==sN || HtmlFormFields[i].name==sN+':0') { 
      HtmlFormFields[i].required=sSTATE;break; 
    }
  }
  EP_HL_REQ_FLD(sN); 
}

function EFormGetReadonly(sN) {
  var rv=false;
  for (var i = 0; i < HtmlFormFields.length; i++) {
    if (HtmlFormFields[i].name==sN || HtmlFormFields[i].name==sN+':0') { 
      if (HtmlFormFields[i].readonly==1) rv=true; break; 
    }
  }  
  return rv;
}

function EFormSetReadonly(sN,sSTATE) {
  var curFieldID="";
  for (var i = 0; i < HtmlFormFields.length; i++) {
    if (HtmlFormFields[i].name==sN) {    
      curFieldID=HtmlFormFields[i].id;       
      if (sSTATE) HtmlFormFields[i].readonly=1; else HtmlFormFields[i].readonly=0;      
      if (HtmlFormFields[i].type=='select') document.HtmlPlusForms[curFieldID].disabled=sSTATE;      
      if (HtmlFormFields[i].type!='checkbox') {
		document.getElementById(curFieldID).readOnly=sSTATE;
		if (sSTATE) document.getElementById(curFieldID).tabIndex=-1;
		else document.getElementById(curFieldID).tabIndex=HtmlFormFields[i].taborder;
	  }
      else  {
        var ix=0;var bChk=false;var f=document.HtmlPlusForms[sN][ix];
            while (f) {   
               f=document.HtmlPlusForms[sN][ix];
               if (f) f.disabled=sSTATE;         
               ix++;
            }
      }          
      break;
    }
    if (HtmlFormFields[i].name==sN+':0') {	  
      curFieldID=HtmlFormFields[i].id;      
      if (sSTATE) HtmlFormFields[i].readonly=1; else HtmlFormFields[i].readonly=0;      
      document.getElementById(curFieldID).disabled=sSTATE;
      var ix=0;var fb=String(sN);var f = document.HtmlPlusForms[fb+':'+ix];	  
      while (f) { 		
        f = document.HtmlPlusForms[fb+':'+ix];
        if (f) f.disabled=sSTATE;
        ix++;
      }       
      break; 
    }
  }  
  EP_HL_REQ_FLD(sN); 
}


function EFormGetVisible(sN) {
  var rv=false;
  for (var i = 0; i < HtmlFormFields.length; i++) {
    if (HtmlFormFields[i].name==sN || HtmlFormFields[i].name==sN+':0') { 
      if (HtmlFormFields[i].visible==1) rv=true; break; 
    }
  }  
  return rv;
}

function EFormSetVisible(sN,sSTATE) {
  var curFieldID=""; var sT="";if (sSTATE==true) sT='visible'; else sT='hidden'; 
  for (var i = 0; i < HtmlFormFields.length; i++) {
    if (HtmlFormFields[i].name==sN) {
      curFieldID=HtmlFormFields[i].id; 
      if (sSTATE) HtmlFormFields[i].visible=1; else HtmlFormFields[i].visible=0;      
      if (HtmlFormFields[i].type=='select') document.HtmlPlusForms[curFieldID].style.visibility=sT;      
      if (HtmlFormFields[i].type!='checkbox') document.getElementById(curFieldID).style.visibility=sT;
      else {
        var ix=0;var bChk=false;var f=document.HtmlPlusForms[sN][ix];
            while (f) {   
               f=document.HtmlPlusForms[sN][ix];
               if (f) f.style.visibility=sT;ix++;
            }
      }          
      break;
    }
    if (HtmlFormFields[i].name==sN+':0') {
      curFieldID=HtmlFormFields[i].id; 
      if (sSTATE) HtmlFormFields[i].visible=1; else HtmlFormFields[i].visible=0;      
      document.getElementById(curFieldID).style.visibility=sT;
      var ix=0;var fb=String(HtmlFormFields[i].name);fb=fb.substring(0,fb.indexOf(':'));var f = document.HtmlPlusForms[fb+':'+ix]; 
      while (f) {                         
        f = document.HtmlPlusForms[fb+':'+ix];
        if (f) f.style.visibility=sT;
        ix++;
      }       
      break; 
    }
  }  
  EP_HL_REQ_FLD(sN); 
}

function EFormGetField(sN) {
	// ,sEXP
    sN = GetFormFieldIDByName(sN);
    var f = document.HtmlPlusForms[sN];
    if (!f) 
    {
      var fb=String(sN);fb=fb.substring(0,fb.indexOf(':'));
      f = document.HtmlPlusForms[fb+':0'];
    }          	
	return f;	 
}

function EFormSetFocus(f,bX) {
      for (var i = 0; i < HtmlFormFields.length; i++) {
        if (f.name!=undefined) {
          if (HtmlFormFields[i].name==f.name) {          
            if (EP_PAGEMODE==1||EP_PAGEMODE==2) EFormSetPageFocus(parseInt(HtmlFormFields[i].page)+1,bX);
			
			if (HtmlFormFields[i].type=='signature') {
				try {
				if (EP_BRWSVAR.indexOf('Internet Explorer')<0) var timer = setTimeout("try {document.getElementById('b" + f.id + "').focus();document.getElementById('b" + f.id + "').select(); } catch(e) {} ",50); else {document.HtmlPlusForms['b' + f.id].focus();document.HtmlPlusForms['b' + f.id].select();} 
				}
				catch (e) { }
			}
			else
			{
				if (EP_BRWSVAR.indexOf('Internet Explorer')<0) var timer = setTimeout("try {document.getElementById('" + f.id + "').focus();document.getElementById('" + f.id + "').select(); } catch(e) {} ",50); else {f.focus();f.select();} 
			}
			
            break;
           }
        }
        else {
          if (HtmlFormFields[i].name==f[0].name) {          
            if (EP_PAGEMODE==1||EP_PAGEMODE==2) EFormSetPageFocus(parseInt(HtmlFormFields[i].page)+1,bX);
			if (EP_BRWSVAR.indexOf('Internet Explorer')<0) var timer = setTimeout("try {document.getElementById('" + f[0].id + "').focus();document.getElementById('" + f[0].id + "').select();} catch(e) { }",50); else {f[0].focus();f[0].select();} 
            break;
          }
        }        
      }    
}

function EFormSetPageFocus(tab,bX) {
  var bIncr=false;  
  if (dpg >= EP_VIEWPAGE) bIncr=true; 
  var dpg = parseInt(tab);
  if (EFormGetPageVisible(dpg)) {
    for(i=0; i < tabs.length; i++) {
      var obj = document.getElementById(tabs[i]);
      obj.style.display = "none";
      var obj = document.getElementById(tabs[i] + 'b');                
      obj.style.backgroundColor = "#DDDDDD";
      var obj = document.getElementById('tab' + (i+1) +'b');                
      obj.style.zIndex=i; 
      obj.style.fontWeight = 'normal';        
    }
    EP_VIEWPAGE = dpg;
    var obj = document.getElementById('tab' + EP_VIEWPAGE);
    obj.style.display = "block";obj.style.border = "0px";obj.style.borderStyle = "none";
    obj.style.borderWidth = "0px";obj.style.margin = "0px";obj.style.padding = "0px";          
    var obj = document.getElementById('tab' + EP_VIEWPAGE + 'b');                
    obj.style.backgroundColor = "#FFFFFF";
    obj.style.zIndex=EP_PAGECNT+1;
    obj.style.fontWeight = 'bold';
  } 
  if (bX==undefined||!bX) setTimeout("window.scrollTo(0,0);",10);  
}

function EFormSetPageVisible(pg,sSTATE) {
   if (sSTATE==false) { 
     if (EP_VIEWPAGE == parseInt(pg)) { 
           for(i=1;i< EP_PAGECNT; i++) {
              if (EFormGetPageVisible(i)) {
                 EFormSetPageFocus(i);break;               
              }                         
           }
     }
     var obj = document.getElementById('tab' + parseInt(pg) + 'b'); obj.style.display = "none"; 
  }
  else {
      var obj = document.getElementById('tab' + parseInt(pg) + 'b'); obj.style.display = "block";           
  }
}

function EFormGetPageVisible(pg) {
  var sRet=false; var obj = document.getElementById('tab' + parseInt(pg) + 'b');
  if (obj.style.display != 'none') sRet=true; 
  return sRet;
}

function EFormGetGeolocation(f1,f2) {
	try {
	var lat=0;var lon=0; var x = navigator.geolocation.getCurrentPosition (   
	function(pos) {			
		lat = pos.coords.latitude;lon = pos.coords.longitude;													
		if (f2!=undefined) { EFormSetValue(f1,lat); EFormSetValue(f2,lon); }
		else EFormSetValue(f1,lat + "," + lon); 
	});
	}
	catch (e) {
	}
}

function EFormOpenDialog(sC,sT,iT,iC,sEO,sE,iW,iH,bHX) {
	jAlert(sC,sT,sE,iT,iC,sEO,iW,iH,bHX);
	//sC: alert html content
	//sT: alert caption
	//sE: js on confirm
	//iT=0 OK only 		sE (if present) is always executed		|		iT=1 OK + Cancel 	confirmation, sE executes only if OK pressed
	//iC=0 Error 		ui-icon-circle-close		|		iC=1 Warning 		ui-icon-notice		default		|		iC=2 Question 	ui-icon-help		|		iC=3 Status 		ui-icon-circle-check 	
	//sEO: js on open
	//iW: pixel width		if not specified falls back to default
	//iH: pixel heigth		if not specified falls back to default
}

